<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * Created by PhpStorm.
 * User: zhengzifan
 * Date: 2017/3/16
 * Time: 13:41
 */

function getAlert($msg, $url=null){
    $html = '';
    $html .= "<script>";
    if($msg){
        $html .= "alert('{$msg}');";
    }
    if($url){
        $html .= "window.location.href='{$url}';";
    }
    $html .= "</script>";
    echo $html;
}

/**
 * @param $info
 * 写入管理员操作日志
 */
function admin_log($info){
    db('AdminLog')->insert([
        'admin_id' => session('admin')['id'],
        'log_info' => $info,
        'log_ip' => $_SERVER['REMOTE_ADDR'],
        'log_time' => time(),
    ]);
}

function checkOrders(){
    $order_config = db('CommonEnum')->where(['mode_name'=>'订单配置','enum_name'=>'归还时限'])->find();
    $limitLen = time() - $order_config['score'] * 3600;
    db('Order')->where(['start_time'=>['<=',$limitLen],'order_mode'=>1,'order_status'=>4,'hire_status'=>2])->update(['order_status'=>5,'hire_status'=>6,'end_time'=>time()]);
}

function getImage($url,$save_dir='',$filename='',$type=0){
    if(trim($url)==''){
        return array('file_name'=>'','save_path'=>'','error'=>1);
    }
    if(trim($save_dir)==''){
       $save_dir='./';
    }
    if(trim($filename)==''){//保存文件名
        $filename = 'qr'.time().'.jpg';
    }
    if(0!==strrpos($save_dir,'/')){
        $save_dir.='/';
    }
    //获取远程文件所采用的方法
    if($type){
        $ch=curl_init();
        $timeout=5;
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
        $img=curl_exec($ch);
        curl_close($ch);
    }else{
        ob_start();
        readfile($url);
        $img=ob_get_contents();
        ob_end_clean();
    }
    $basePath = str_replace('\\','/',realpath('./'));
    //$size=strlen($img);
    //文件大小
    $filepath = $basePath.$save_dir.$filename;
    $fp2=@fopen($filepath,'a');
    dump($filepath);
    fwrite($filepath,$img);
    fclose($filepath);
    unset($img,$url);
    return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0);
  }